<?php
defined( 'BASEPATH' ) or exit( 'No direct script access allowed' );
class Cache_handle extends MY_Controller
{
    function clear()
    {
        $dir = $this->config->item( 'cache_path' );
        if ($this->delete_dir( $dir, false ))
        {
            $this->rs->ok();
        }
        $this->rs->err();
    }
    private function delete_dir($dir, $self = true)
    {
        $result = true;
        // 先删除目录下的文件
        $handle = opendir( $dir );
        while ( ($file = readdir( $handle )) !== false )
        {
            if (! $self && ($file == '.htaccess' || $file == 'index.html'))
            {
                continue;
            }
            if ($file != "." && $file != "..")
            {
                $path = $dir . "/" . $file;
                if (! is_dir( $path ))
                {
                    $result = $result && unlink( $path );
                }
                else
                {
                    $result = $result && $this->delete_dir( $path );
                }
            }
        }
        closedir( $handle );
        if ($self)
        {
            // 删除当前文件夹
            return $result && rmdir( $dir );
        }
        else
        {
            return $result;
        }
    }
}